home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 551-575 / disk_572 / wbstart / wbstarter.c < prev   
C/C++ Source or Header  |  1992-05-06  |  2KB  |  111 lines

  1. /*
  2.  * WBStarter.c   V1.0
  3.  *
  4.  * Start WB programs via WBStart-Handler
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "WBStart.h"
  10. #include <clib/dos_protos.h>
  11. #include <clib/exec_protos.h>
  12. #include <dos/dostags.h>
  13. #include <workbench/startup.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16.  
  17. /* Version string */
  18. static char Version[]="$VER: WBStarter V1.0 (24.11.1991)";
  19.  
  20. void main(int argc, char *argv[])
  21. {
  22.  BPTR fl;
  23.  struct WBStartMsg msg;
  24.  struct MsgPort *mp,*hp;
  25.  
  26.  if (!(mp=CreateMsgPort()))
  27.   {
  28.    puts("No message port!\n");
  29.    exit(20);
  30.   }
  31.  
  32.  fl=CurrentDir(NULL);
  33.  msg.wbsm_Msg.mn_Node.ln_Pri=0;
  34.  msg.wbsm_Msg.mn_ReplyPort=mp;
  35.  msg.wbsm_DirLock=fl;
  36.  msg.wbsm_Stack=4096;
  37.  msg.wbsm_Prio=0;
  38.  msg.wbsm_NumArgs=0;
  39.  msg.wbsm_ArgList=NULL;
  40.  
  41.  while (--argc)
  42.   {
  43.    msg.wbsm_Name=*++argv;
  44.  
  45.    /* Try to send a message to the WBStart-Handler */
  46.    Forbid();
  47.    hp=FindPort(WBS_PORTNAME);
  48.    if (hp) PutMsg(hp,(struct Message *) &msg);
  49.    Permit();
  50.  
  51.    /* No WBStart-Handler, try to start it! */
  52.    if (!hp)
  53.     {
  54.      BPTR ifh=Open("NIL:",MODE_NEWFILE);
  55.      BPTR ofh=Open("NIL:",MODE_OLDFILE);
  56.  
  57.      /* Start handler */
  58.      if (SystemTags(WBS_LOADNAME,SYS_Input,ifh,
  59.                                  SYS_Output,ofh,
  60.                                  SYS_Asynch,TRUE,
  61.                                  SYS_UserShell,TRUE,
  62.                                  NP_ConsoleTask,NULL,
  63.                                  NP_WindowPtr,NULL,
  64.                                  TAG_DONE)!=-1)
  65.       {
  66.        int i;
  67.  
  68.        /* Handler started, try to send message (Retry up to 5 seconds) */
  69.        for (i=0; i<10; i++)
  70.         {
  71.          /* Try to send message */
  72.          Forbid();
  73.          hp=FindPort(WBS_PORTNAME);
  74.          if (hp) PutMsg(hp,(struct Message *) &msg);
  75.          Permit();
  76.  
  77.          /* Message sent? Yes, leave loop */
  78.          if (hp) break;
  79.  
  80.          /* No, wait 1/2 second */
  81.          Delay(25);
  82.         }
  83.       }
  84.      else
  85.       {
  86.        /* Handler not started, close file handles */
  87.        Close(ifh);
  88.        Close(ofh);
  89.       }
  90.     }
  91.  
  92.    /* Could we send the message? */
  93.    if (hp)
  94.     {
  95.      /* Get reply message */
  96.      WaitPort(mp);
  97.      GetMsg(mp);
  98.     }
  99.    else
  100.     {
  101.      /* Oops. ERROR! */
  102.      puts("Can't find 'WBStart-Handler'!");
  103.      break;
  104.     }
  105.   }
  106.  
  107.  /* Free resources */
  108.  CurrentDir(fl);
  109.  DeleteMsgPort(mp);
  110. }
  111.